<!DOCTYPE html>
<html ng-app="demoApp">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>TinyMCE 富文本 - ng-components-ui</title>
    <link rel="stylesheet" href="../dist/ng-components.css" />
    <style>
      * {
        box-sizing: border-box;
      }

      body {
        font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
          "Helvetica Neue", Arial, sans-serif;
        margin: 0;
        padding: 0;
        background: #f5f5f5;
      }

      .header {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        padding: 30px 40px;
        box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
      }

      .header h1 {
        margin: 0 0 10px 0;
        font-size: 32px;
      }

      .header p {
        margin: 0;
        opacity: 0.9;
        font-size: 16px;
      }

      .back-link {
        display: inline-block;
        margin-top: 15px;
        color: white;
        text-decoration: none;
        padding: 8px 16px;
        background: rgba(255, 255, 255, 0.2);
        border-radius: 4px;
        transition: background 0.2s;
      }

      .back-link:hover {
        background: rgba(255, 255, 255, 0.3);
      }

      .container {
        max-width: 1200px;
        margin: 0 auto;
        padding: 40px;
      }

      .demo-section {
        background: white;
        padding: 30px;
        border-radius: 8px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
        margin-bottom: 30px;
      }

      .demo-section h2 {
        margin: 0 0 20px 0;
        color: #333;
        font-size: 20px;
        border-bottom: 2px solid #667eea;
        padding-bottom: 10px;
      }

      .demo-section h3 {
        margin: 30px 0 15px 0;
        color: #555;
        font-size: 16px;
        font-weight: 600;
      }

      .demo-section p {
        color: #666;
        line-height: 1.6;
        margin: 0 0 15px 0;
      }

      .feature-list {
        background: #f7fafc;
        border-left: 4px solid #667eea;
        padding: 15px 20px;
        margin: 20px 0;
      }

      .feature-list ul {
        margin: 0;
        padding-left: 20px;
      }

      .feature-list li {
        color: #4a5568;
        margin: 8px 0;
        line-height: 1.6;
      }

      .demo-actions {
        display: flex;
        gap: 12px;
        flex-wrap: wrap;
        margin: 12px 0 0 0;
      }

      .btn {
        padding: 10px 20px;
        border: none;
        border-radius: 6px;
        cursor: pointer;
        font-size: 14px;
        font-weight: 500;
        transition: all 0.2s;
      }

      .btn-primary {
        background: #667eea;
        color: white;
      }

      .btn-success {
        background: #38a169;
        color: white;
      }

      .btn-info {
        background: #4299e1;
        color: white;
      }

      .btn-warning {
        background: #ed8936;
        color: white;
      }

      .debug-output {
        background: #2d3748;
        color: #e2e8f0;
        padding: 15px;
        border-radius: 6px;
        font-family: "Monaco", "Menlo", "Ubuntu Mono", monospace;
        font-size: 13px;
        margin-top: 15px;
      }

      .debug-output strong {
        color: #90cdf4;
        display: block;
        margin-bottom: 10px;
      }

      .debug-output pre {
        margin: 0;
        white-space: pre-wrap;
        word-break: break-all;
      }

      .code-block {
        background: #f7fafc;
        border: 1px solid #e2e8f0;
        border-radius: 4px;
        padding: 15px;
        margin: 15px 0;
        font-family: "Monaco", "Menlo", "Ubuntu Mono", monospace;
        font-size: 12px;
        overflow-x: auto;
      }

      .tinymce-status {
        margin-top: 10px;
        padding: 10px;
        border-radius: 6px;
        background: #edf2ff;
        color: #3b5bdb;
        font-size: 13px;
      }

      .tinymce-guide {
        background: #f7fafc;
        border-left: 4px solid #667eea;
        padding: 12px 16px;
        border-radius: 6px;
        font-size: 13px;
        color: #4a5568;
        margin: 10px 0;
      }

      .tinymce-guide ul {
        margin: 8px 0 0 16px;
        padding: 0;
      }

      .tinymce-guide li {
        margin-bottom: 6px;
        line-height: 1.4;
      }
    </style>
  </head>
  <body ng-controller="DemoController">
    <div class="header">
      <h1>TinyMCE 富文本组件</h1>
      <p>使用 ng-tinymce 指令封装的 TinyMCE 编辑器</p>
      <a href="home.html" class="back-link">← 返回主页</a>
    </div>

    <div class="container">
      <div class="demo-section">
        <h2>组件能力</h2>

        <div class="feature-list">
          <strong>功能特性:</strong>
          <ul>
            <li>自动按需加载 TinyMCE 主脚本，无需在页面手动引用</li>
            <li>支持完整 TinyMCE 配置透传，包含插件、语言、皮肤、工具栏</li>
            <li>原生支持 AngularJS `ngModel` 双向绑定、占位符、只读等属性</li>
            <li>`on-init` 回调暴露原生 editor 实例，方便执行高级操作</li>
          </ul>
        </div>

        <h3>1. 基础用法</h3>
        <p>最简洁的富文本场景，输入的 HTML 与 `ngModel` 实时互通，默认内容内置链接与图片</p>
        <div class="tinymce-guide">
          <strong>操作指引：</strong>
          <ul>
            <li>点击「TinyMCE 官网文档」链接，观察富文本中锚点的默认样式，可在“预览”中检查跳转结果。</li>
            <li>双击示例图片唤起大图预览，再尝试设置居中、加描边或添加说明文字，了解图片在编辑器中的行为。</li>
            <li>借助工具栏的链接 / 图片按钮自行插入更多素材，并查看 HTML 输出。</li>
          </ul>
        </div>
        <ng-tinymce
          ng-model="tinymce.basic"
          placeholder="请输入正文..."
          image-preview-enabled="true"
          on-init="handleTinyInit($editor, 'basic')"
        ></ng-tinymce>
        <div class="tinymce-status" ng-if="tinymceStatus.basic">
          {{ tinymceStatus.basic }}
        </div>
        <div class="demo-actions">
          <button class="btn btn-primary" ng-click="insertBlock('basic')">
            插入段落
          </button>
          <button class="btn btn-warning" ng-click="resetBasic()">
            重置内容
          </button>
        </div>
        <div class="debug-output">
          <strong>绑定内容:</strong>
          <pre>{{ tinymce.basic }}</pre>
        </div>

        <div class="code-block">
          <code>
&lt;ng-tinymce<br />
&nbsp;&nbsp;ng-model="article.content"<br />
&nbsp;&nbsp;placeholder="请输入正文..."<br />
&nbsp;&nbsp;on-init="handleTinyInit($editor)"&gt;<br />
&lt;/ng-tinymce&gt;
          </code>
        </div>

        <h3>2. 自定义工具栏 / 语言包</h3>
        <p>通过 <code>config</code> 传入 TinyMCE 官方配置，加载中文语言包和全屏预览</p>
        <ng-tinymce
          ng-model="tinymce.custom"
          config="tinymceCustomConfig"
          on-init="handleTinyInit($editor, 'custom')"
        ></ng-tinymce>
        <div class="tinymce-status" ng-if="tinymceStatus.custom">
          {{ tinymceStatus.custom }}
        </div>
        <div class="demo-actions">
          <button class="btn btn-success" ng-click="insertBlock('custom')">
            追加更新项
          </button>
        </div>
        <div class="debug-output">
          <strong>绑定内容:</strong>
          <pre>{{ tinymce.custom }}</pre>
        </div>

        <div class="code-block">
          <code>
$scope.tinymceCustomConfig = {<br />
&nbsp;&nbsp;height: 360,<br />
&nbsp;&nbsp;menubar: true,<br />
&nbsp;&nbsp;plugins: 'preview code lists link image table media fullscreen',<br />
&nbsp;&nbsp;toolbar: 'undo redo | preview fullscreen | styles | bold italic underline',<br />
&nbsp;&nbsp;language_url: 'https://cdn.jsdelivr.net/npm/tinymce-i18n/langs5/zh_CN.js',<br />
&nbsp;&nbsp;language: 'zh_CN'<br />
};<br />
          </code>
        </div>

        <h3>3. 禁用 / 审核模式</h3>
        <p>
          通过 `disabled` 绑定只读状态，并利用
          <code>readonly-config</code> 增补预览 / 全屏 / 代码查看等工具；双击图片同样会弹出大图
        </p>
        <ng-tinymce
          ng-model="tinymce.readonly"
          config="tinymceMiniConfig"
          disabled="tinymceDisabled"
          readonly-config="tinymceReadonlyConfig"
          image-preview-enabled="true"
          on-init="handleTinyInit($editor, 'readonly')"
        ></ng-tinymce>
        <div class="tinymce-status" ng-if="tinymceStatus.readonly">
          {{ tinymceStatus.readonly }}
        </div>
        <div class="demo-actions">
          <button class="btn btn-info" ng-click="toggleDisabled()">
            {{ tinymceDisabled ? "切换为可编辑" : "切换为只读" }}
          </button>
          <button class="btn btn-primary" ng-click="insertBlock('readonly')">
            追加备注
          </button>
        </div>
        <div class="debug-output">
          <strong>绑定内容:</strong>
          <pre>{{ tinymce.readonly }}</pre>
        </div>
      </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/angular@1.8.3/angular.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/angular-strap@2.3.12/dist/angular-strap.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/angular-strap@2.3.12/dist/angular-strap.tpl.min.js"></script>
    <script src="../dist/ng-components.js"></script>
    <script>
      angular
        .module("demoApp", ["ngComponents"])
        .controller("DemoController", function ($scope) {
          var linkBlock =
            '<p>了解更多：<a href="https://www.tiny.cloud/docs/" target="_blank" rel="noopener noreferrer">TinyMCE 官网文档</a></p>';
          var imageBlock =
            '<p><img src="https://picsum.photos/seed/ng-components-tinymce-standalone/640/320" alt="TinyMCE 演示图片" style="max-width:100%;height:auto;border-radius:8px;display:block;margin:12px auto;" /></p>';

          var basicTemplate =
            '<p><strong>欢迎使用 TinyMCE 富文本组件。</strong></p><p>这里是基础示例，任何输入都会实时写回 ngModel。</p>' +
            linkBlock +
            imageBlock;
          var customTemplate =
            '<h3>更新日志</h3><ul><li>支持 config 自定义工具栏和插件</li><li>支持语言包、全屏、代码查看等高级能力</li></ul>' +
            '<p>通过下方链接和图片展示锚点/媒体渲染效果。</p>' +
            linkBlock +
            imageBlock;
          var readonlyTemplate =
            '<p>该内容处于只读模式，通常用于审核详情。</p><p>可通过按钮切换编辑权限。</p>' +
            linkBlock +
            imageBlock;

          $scope.tinymce = {
            basic: basicTemplate,
            custom: customTemplate,
            readonly: readonlyTemplate
          };

          $scope.tinymceStatus = {};
          $scope.tinymceDisabled = true;

          $scope.tinymceCustomConfig = {
            height: 360,
            menubar: true,
            plugins: "preview code lists link image table media fullscreen",
            toolbar:
              "undo redo | preview fullscreen | styles | bold italic underline forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image media table | code",
            language_url: "https://cdn.jsdelivr.net/npm/tinymce-i18n/langs5/zh_CN.js",
            language: "zh_CN"
          };

          $scope.tinymceMiniConfig = {
            height: 260,
            menubar: false,
            plugins: "lists code",
            toolbar:
              "undo redo | bold italic underline | forecolor backcolor | bullist numlist | alignleft aligncenter alignright | removeformat code"
          };

          $scope.tinymceReadonlyConfig = {
            menubar: false,
            toolbar: "preview fullscreen",
            plugins: "fullscreen preview",
            toolbar_sticky: false
          };

          $scope.handleTinyInit = function (editor, key) {
            $scope.$applyAsync(function () {
              $scope.tinymceStatus[key] = "编辑器 #" + editor.id + " 已就绪";
            });
          };

          $scope.insertBlock = function (target) {
            var snippet =
              "<p><strong>插入段落：</strong>" + new Date().toLocaleString() + "</p>";
            $scope.tinymce[target] = ($scope.tinymce[target] || "") + snippet;
          };

          $scope.resetBasic = function () {
            $scope.tinymce.basic = basicTemplate;
          };

          $scope.toggleDisabled = function () {
            $scope.tinymceDisabled = !$scope.tinymceDisabled;
          };
        });
    </script>
  </body>
</html>
